package cn.kailangtu.myapplication.camera;

import android.os.Environment;

import com.example.ipcamstream.IpCamStream;


/**
 * Created by fujf on 2021/12/27
 * Description:
 */
public class SmIpCamera {
    private IpCamStream ipCamStream;
    private IpCamStream.cameraOnFrameListen cameraOnFrameListen;

    private String mp4Path = "1";

    public static long lastReceiveTime = System.currentTimeMillis();

    private boolean isOpen = false;

    private boolean isPause = false;

    public void setPause(boolean isPause)
    {
        this.isPause = isPause;
    }

    public SmIpCamera(String mp4Path, String url, int openMode, int fps, AvcPlayer avc) {
        this.mp4Path = mp4Path;
        ipCamStream = new IpCamStream(url, openMode, fps);
        cameraOnFrameListen = new IpCamStream.cameraOnFrameListen() {
            /**
             * h264裸流取流接口，注意用户不要在此接口中做复杂耗时操作
             * 否则有可能造成取流阻塞出现花屏现象，当用户调用了IpcCamStartWidthOnFrameMp4
             * 接口时不应再在onStream中进行存储操作，否则可能会造成花屏现象
             *
             * @param data      h264流数组
             * @param data_len  数组长度
             * @param bkeyFrame 是否为关键帧
             */
            @Override
            public void onStream(byte[] data, int data_len, boolean bkeyFrame) {
//                Logger.d("摄像头，onStream：" + data_len + " @ " + bkeyFrame);
                lastReceiveTime = System.currentTimeMillis();
                if(!isPause) {
                    avc.decodeNalu(data);
                }
            }

            @Override
            public void onConnect() {

            }

            @Override
            public void onDisConnect() {

            }

            @Override
            public void onAbnormalExit(int errCode, String errString) {

//                openCamera();
                CloseCamera();
            }
        };
    }
    private void openCamera() {
        if (isOpen) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    int errOpen = IpcCamOpen();

                    if (errOpen == 0) {
                        setStartWithOnFrame();
                    } else {
                        openCamera();
                    }
                }
            }).start();
        } else {

        }
    }

    private void setStartWithOnFrame(){
        /*
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SMCamera/";
        path = path + mp4Path + "/";
        String outMp4Path = path + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS").format(System.currentTimeMillis()) + ".mp4";
        File file = new File(path);
        if(! Objects.requireNonNull(file).exists()) {
            file.mkdirs();
        }
        int start = ipCamStream.IpcCamStartWidthOnFrameMp4_ext(cameraOnFrameListen, outMp4Path, 1);

         */
        int start = ipCamStream.IpcCamStartWidthOnFrame(cameraOnFrameListen);

    }

    public void openCamera(CallbackIpcCamOpen callBack) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                int errOpen = IpcCamOpen();

                if (errOpen == 0) {
                    isOpen = true;
                    setStartWithOnFrame();
                    callBack.onOpenSuc(errOpen);
                } else {
                    callBack.onOpenFai(errOpen);
                }
            }
        }).start();
    }

    public void CloseCamera() {
        int close = IpcCamClose();
        isOpen = false;

    }

    public interface CallbackIpcCamOpen {
        void onOpenSuc(int errCode);
        void onOpenFai(int errCode);
    }

    private int IpcCamOpen(){
        int code = -1;
        try {
            code = ipCamStream.IpcCamOpen();
        } catch (Exception e) {

        }
        return code;
    }
    private int IpcCamClose(){
        int code = -1;
        try {
            code = ipCamStream.IpcCamClose();
        } catch (Exception e) {

        }
        return code;
    }
    
    

}
